package edu.sdjzu.controller;

import edu.sdjzu.dto.CreateArticleRequest;
import edu.sdjzu.entity.Article;
import edu.sdjzu.service.ContentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/content")
public class ContentController {

    private final ContentService contentService;

    public ContentController(ContentService contentService) {
        this.contentService = contentService;
    }

    @GetMapping("/test")
    public String test() {
        return "blog-content 被调用了！";
    }

    @PostMapping("/articles")
    public ResponseEntity<?> createArticle(@RequestBody CreateArticleRequest request) {
        contentService.createArticle(request);
        return ResponseEntity.ok("Article created");
    }

    @GetMapping("/articles/{id}")
    public ResponseEntity<Article> getArticle(@PathVariable("id") Long id) {
        return ResponseEntity.ok(contentService.getArticleById(id));
    }

    @GetMapping("/articles")
    public ResponseEntity<List<Article>> getArticlesByAuthor(@RequestParam("userId") Long userId) {
        return ResponseEntity.ok(contentService.getArticlesByUserId(userId));
    }

}
